package com.mic.yzzn.plc.common.instructions.request;

import com.mic.yzzn.plc.common.ins.datastructure.PLCContactorNumber;
import com.mic.yzzn.plc.common.instructions.basic.PLCRequestInstruction;
import org.apache.commons.lang.StringUtils;

/**
 * Created by ASH on 16/3/7.
 */
public class PLCSingleContactorReadInstruction extends PLCSingleContactorInstruction
{
	/**
	 * 初始化方法
	 */
    public PLCSingleContactorReadInstruction()
    {
        new PLCSingleContactorInstruction();

        requestCode = PLCRequestInstruction.READ_SINGLE_CONTACTOR_INS;
    }
    
    @Override
    public String initWithString(String instructionString) throws IllegalArgumentException
    {
    	String tempResult = super.initWithString(instructionString);
    	
//    	System.out.println(tempResult);

    	// 判断请求码与当前指令的请求码是否符合
    	int requestCodeLength = requestCode.length();
    	if (!(StringUtils.substring(tempResult, 0, requestCodeLength).equals(requestCode)))
		{
			throw new IllegalArgumentException("PLCSingleContactorReadInstruction's requestCode must be \"" + requestCode +"\"");
		}
    	
    	// 截取请求码
    	tempResult = StringUtils.substringAfter(tempResult, requestCode);
    	
    	// 获得触点代码
    	String contactorCode = tempResult.substring(0, 1);
    	
    	this.contactorCode = contactorCode;
    	
    	// 截断单触点代码
    	tempResult = StringUtils.substringAfter(tempResult, contactorCode);
    	
    	int leftLength = tempResult.length();
    	
    	// 获取触点编号
    	PLCContactorNumber contactorNumber = new PLCContactorNumber(tempResult.substring(0, leftLength - 1), 
    			tempResult.charAt(leftLength - 1));
    	
    	// 设置触点编号
    	this.contactorNumber = contactorNumber;
    	
    	// 截断触点编号
    	tempResult = StringUtils.substringAfter(tempResult, contactorNumber.toString());
    	
    	return tempResult;
    }
}
